﻿using DICOM_PROJECT.DAL.Security;
using DICOM_PROJECT.Models;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using DICOM_PROJECT.Extension;

namespace DICOM_PROJECT.Controllers
{
    [CustomAuthorize(Roles = "Doctor")]
    public class DoctorController : BaseController
    {
        //
        // GET: /Doctor/
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public string GetPatientInfo(SearchModel search)
        {
            if (!ModelState.IsValid)
            {
                return "invalid_id";
            }
            var user = new UserProfile();
            if (search.type == 1)//search with patientID
            {
                user = db.UserProfiles.FirstOrDefault(m => m.UserProfileID == search.id);
            }
            else
            {
                user = db.UserProfiles.FirstOrDefault(m => m.SocialID == search.id.ToString() || m.SocialID == "0" + search.id.ToString());
            }
            if (user == null)
            {
                return "no_matched_data";
            }
            var userResult = new List<PatientModel>
            {
                new PatientModel { ID=user.UserProfileID, SocialID=user.SocialID, Name = user.Name, Birthday=user.Birthday, Male=user.Male, Address = user.Address, Phone = user.Phone }
            };

            //var visits = user.Visits.OrderByDescending(v => v.VisitID).ToPagedList(1, 2).ToList();
            //var visits = from s in db.Visits select s;
            //visits = visits.Where();
            PageModel visitPage = new PageModel { RowsPerPage = 10, PageNumber = 1 };
            var visits = Pagination<Visit>.Page(user.Visits.OrderByDescending(v => v.VisitID), visitPage);
            var visitResult = new List<VisitModel>();
            visits.ForEach(v => visitResult.Add(new VisitModel { VisitID = v.VisitID, CreateDate = v.CreateDate, UpdateDate = v.UpdateDate, Symptom = v.Symptom }));

            //var indications = user.Indications.OrderByDescending(i => i.IndicationID).ToPagedList(1, 2).ToList();
            PageModel indicationPage = new PageModel { RowsPerPage = 10, PageNumber = 1 };
            var indications = Pagination<Indication>.Page(user.Indications.OrderByDescending(i => i.IndicationID), indicationPage);
            var indicationResult = new List<IndicationModel>();
            indications.ForEach(i => indicationResult.Add(new IndicationModel { IndicationID = i.IndicationID, CreateDate = i.CreateDate, UpdateDate = i.UpdateDate, IndicationInformation = i.IndicationInformation }));

            var result = new ArrayList();
            result.Add(userResult);
            result.Add(visitResult);
            result.Add(visitPage);
            result.Add(indicationResult);
            result.Add(indicationPage);

            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return serializer.Serialize(result);
        }

        [HttpPost]
        public string CreateIndication(IndicationCreateModel newIndication)
        {
            db.Indications.Add(new Indication{UserProfileID=newIndication.PatientID, DoctorID=User.UserID, CreateDate=DateTime.Now, IndicationInformation=newIndication.IndicationInformation});
            db.SaveChanges();
            //var indication = db.Indications.OrderByDescending(i => i.IndicationID).First();
            //var result = new ArrayList();
            //result.Add(new IndicationModel { IndicationID = indication.IndicationID, CreateDate = indication.CreateDate, UpdateDate = indication.UpdateDate, IndicationInformation = indication.IndicationInformation });

            //JavaScriptSerializer serializer = new JavaScriptSerializer();
            //return serializer.Serialize(result);
            return GetIndicationsPage(new PageModel { PatientID = newIndication.PatientID, RowsPerPage = 10, PageNumber = 1 });
        }

        [HttpPost]
        public string GetIndication(string id)
        {
            int indicationID = Int32.Parse(id);
            var indication = db.Indications.Find(indicationID);
            var result = new ArrayList();
            result.Add(new IndicationViewModel { 
                IndicationID=indication.IndicationID,
                PatientID = indication.UserProfileID,
                PatientName=indication.UserProfile.Name,
                DoctorID=indication.DoctorID, 
                DoctorName=indication.Doctor!=null? indication.Doctor.Name:null,
                CreateDate = indication.CreateDate.ToString("HH:mm:ss dd/MM/yyyy"),
                IndicationInformation = indication.IndicationInformation,
                RadiologistID = indication.RadiologistID,
                RadiologistName = indication.Radiologist!=null? indication.Radiologist.Name:null,
                UpdateDate = indication.UpdateDate != null ? ((DateTime)indication.UpdateDate).ToString("HH:mm:ss dd/MM/yyyy") : null,
                IndicationResult=indication.IndicationResult
            });
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return serializer.Serialize(result);
        }

        [HttpPost]
        public string CreateVisit(VisitCreateModel newVisit)
        {
            db.Visits.Add(new Visit { UserProfileID = newVisit.PatientID, CreateDoctorID = User.UserID, CreateDate = DateTime.Now, Symptom = newVisit.Symptom, Diagnosis = newVisit.Diagnosis, Treatment = newVisit.Treatment });
            db.SaveChanges();
            //var visit = db.Visits.OrderByDescending(i => i.VisitID).First();
            //var result = new ArrayList();
            //result.Add(new VisitModel { VisitID = visit.VisitID, CreateDate = visit.CreateDate, UpdateDate = visit.UpdateDate, Symptom = visit.Symptom });

            //JavaScriptSerializer serializer = new JavaScriptSerializer();
            //return serializer.Serialize(result);
            return GetVisitsPage(new PageModel { PatientID=newVisit.PatientID, RowsPerPage = 10, PageNumber = 1 });
        }

        [HttpPost]
        public string GetVisit(string id)
        {
            int visitID = Int32.Parse(id);
            var visit = db.Visits.Find(visitID);
            var result = new ArrayList();
            result.Add(new VisitUpdateModel
            {
                VisitID = visit.VisitID,
                PatientID = visit.UserProfileID,
                PatientName = visit.UserProfile.Name,
                CreateDoctorID = visit.CreateDoctorID,
                CreateDoctorName = visit.CreateDoctor != null ? visit.CreateDoctor.Name : null,
                CreateDate = visit.CreateDate.ToString("HH:mm:ss dd/MM/yyyy"),
                UpdateDoctorID = visit.UpdateDoctorID,
                UpdateDoctorName = visit.UpdateDoctor != null ? visit.UpdateDoctor.Name : null,
                UpdateDate = visit.UpdateDate !=null ? ((DateTime)visit.UpdateDate).ToString("HH:mm:ss dd/MM/yyyy") : null,
                Symptom = visit.Symptom,
                Diagnosis = visit.Diagnosis,
                Treatment = visit.Treatment
            });
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return serializer.Serialize(result);
        }

        [HttpPost]
        public string SaveVisit(VisitUpdateModel updateVisit)
        {
            var updateSQL = @"UPDATE Visit SET UpdateDoctorID=@p0, UpdateDate=@p1, Symptom=@p2, Diagnosis=@p3, Treatment=@p4 WHERE VisitID=@p5";
            var rowCount = db.Database.ExecuteSqlCommand(updateSQL, User.UserID, DateTime.Now, updateVisit.Symptom, updateVisit.Diagnosis, updateVisit.Treatment, updateVisit.VisitID);
            if (rowCount < 1) {//update fail
                return "update_fail";
            }
            var visitResult = db.Visits.Find(updateVisit.VisitID);
            var result = new VisitModel { VisitID = visitResult.VisitID, CreateDate = visitResult.CreateDate, UpdateDate = visitResult.UpdateDate, Symptom = visitResult.Symptom };
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return serializer.Serialize(result);
        }

        [HttpPost]
        public string GetVisitsPage(PageModel page)
        {
            var patient = new UserProfile();
            patient = db.UserProfiles.FirstOrDefault(m => m.UserProfileID == page.PatientID);
            PageModel visitPage = new PageModel { RowsPerPage = page.RowsPerPage, PageNumber = page.PageNumber };
            var visits = Pagination<Visit>.Page(patient.Visits.OrderByDescending(v => v.VisitID), visitPage);
            var visitResult = new List<VisitModel>();
            visits.ForEach(v => visitResult.Add(new VisitModel { VisitID = v.VisitID, CreateDate = v.CreateDate, UpdateDate = v.UpdateDate, Symptom = v.Symptom }));
            var result = new ArrayList();
            result.Add(visitResult);
            result.Add(visitPage);

            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return serializer.Serialize(result);
        }

        [HttpPost]
        public string GetIndicationsPage(PageModel page)
        {
            var patient = new UserProfile();
            patient = db.UserProfiles.FirstOrDefault(m => m.UserProfileID == page.PatientID);
            PageModel indicationPage = new PageModel { RowsPerPage = page.RowsPerPage, PageNumber = page.PageNumber };
            var indications = Pagination<Indication>.Page(patient.Indications.OrderByDescending(v => v.IndicationID), indicationPage);
            var indicationResult = new List<IndicationModel>();
            indications.ForEach(i => indicationResult.Add(new IndicationModel { IndicationID = i.IndicationID, CreateDate = i.CreateDate, UpdateDate = i.UpdateDate, IndicationInformation = i.IndicationInformation }));
            var result = new ArrayList();
            result.Add(indicationResult);
            result.Add(indicationPage);

            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return serializer.Serialize(result);
        }
    }
}